
          /*
           -------ProductDao By Raj Vallamsetla------
  		   
				For the insertion of Base Product 

          */

package com.scalar.chiptrack.operations.process.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.utils.DBLoader;
import com.scalar.chiptrack.utils.SqlQueryMappings;

public class TestProcessSplitDAO 
{

 private static DBLoader s_dbLoader = null;

 
     public TestProcessSplitDAO()
	  {
	
	  }




	public String getTest(String trackId) throws ScalarException{


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String checkTest = "";
                

		
		try{
				
		
        conn = DataSource.getConnection();                    
		//System.out.println("Conection "+conn);
		pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.selectTest"));
		pstmt.setString(1,trackId);
		rs=pstmt.executeQuery();

		while(rs.next()){
		checkTest ="exists";		     
		}


	}
			catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not getTest from database", sqle);
        }


			 	finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
			
			return checkTest;
		
        }	


	public void save(String assemblyTrackId,String testName,String sequence,String binSetup,String status) throws ScalarException {


				 
				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
              
				 		

		try{
				
								  
           conn = DataSource.getConnection();    
			
			//System.out.println("TestName in Save  "+testName);
			
		  pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testProcessInsert"));
		  pstmt.setString(1,assemblyTrackId);
		  pstmt.setString(2,testName);
		  pstmt.setString(3,sequence);
		  pstmt.setString(4,binSetup);
		  pstmt.setString(5,status);
		  //System.out.println("Debug1  "+testName);	
		  pstmt.executeUpdate(); 
   		  conn.commit();
		  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save Test Process Split database", sqle);
        }


           finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }
        } 

		
	public void saveClassStore(String assemblyTrackId,String status) throws ScalarException{

         Connection conn = null;
         ResultSet rs    = null;
         PreparedStatement pstmt=null;
         // changed by hareesh on 6th October 2003
         //String type="ClassStore";
         String type = "Class Store";

		 try{

          conn = DataSource.getConnection();
		  pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testProcessInsertClassStore"));
		  pstmt.setString(1,assemblyTrackId);
		  pstmt.setString(2,type);
		  pstmt.setString(3,status);
		  pstmt.setString(4,"9999");
		  pstmt.executeUpdate(); 
   		  conn.commit();
		  
		 }
    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save Class Store database", sqle);
        }
        finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
		}
    }

     public ArrayList getTestNames() throws ScalarException{

			 ArrayList testNameList = new ArrayList();


				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String testName="";
				                  
		try{
				
								  
         conn = DataSource.getConnection();  
		 pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.selectTestNames"));
		 rs =  pstmt.executeQuery();

			while(rs.next())
			{
				testName=rs.getString(1);
				testNameList.add(testName);
				

			}
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not get test names database", sqle);
        }


            finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
			}

				return testNameList;	

        }



	public String getTestName(String testName) throws ScalarException{

				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String newtestName="";
			
				                  
		try{
				
								  
		 conn = DataSource.getConnection();  
		 pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.checkTestName"));
		 pstmt.setString(1,testName);
		 rs =  pstmt.executeQuery();

			while(rs.next())
			{
				newtestName=rs.getString(1);
				
			}

			//System.out.println("checkTestName in get Test name "+testName);

		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not get Test Name from database", sqle);
        }


            finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
			}

				return newtestName;	

        }



	public void saveTestName(String testName) throws ScalarException {


				 
				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
                 
				 String  checkTestName =  getTestName(testName);
			
				//System.out.println("checkTestName in Save Test name "+checkTestName);


			if(checkTestName.equals(""))
		{

		try{
				
		  conn = DataSource.getConnection();  
		  pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testNameInsert"));
		  pstmt.setString(1,testName);
		  

		  pstmt.executeUpdate(); 
   		  conn.commit();
		  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not save Test Name Data Base", sqle);
        }


			finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
			}

		}
	}	




	public void updateBinSetUp(String assemblyTrackId) throws ScalarException  {


				 
				 Connection conn = null;
				 ResultSet rs    = null;
				 PreparedStatement pstmt=null;
				 String maxSequence ="";
                 
				
					
		try{
				
								  
          conn = DataSource.getConnection(); 
          pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testMaxSequence"));
		  pstmt.setString(1,assemblyTrackId);
		  rs =  pstmt.executeQuery(); 
		
		  while (rs.next())
		  {
			  maxSequence = rs.getString(1);
		  }	
		  
		  pstmt.close();
		  
		  pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testUpDateBin"));	
			
		  pstmt.setString(1,maxSequence);
		  pstmt.setString(2,assemblyTrackId);
		  pstmt.executeUpdate();
   		  conn.commit();
		  
		 }

	    	catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not update bin setup to  database", sqle);
        }


            finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
			}
      
		
	}	


/*
	Method checkTestTrackId added By Raj Vallamsetla on 07/15/2002 2:08 PM	

*/

public String  checkTestTrackId(String[] assemblyTrackList) throws ScalarException
    {
		
		         Connection conn = null;
				 PreparedStatement pstmt = null;
				 ResultSet rs    = null;
				 String trackId   = "";
				 String assemblyTrackId = "";
							 
		try{
				
					  
       conn = DataSource.getConnection();
	   pstmt = conn.prepareStatement(SqlQueryMappings.get ("operations.process.product.testTrackId"));

	  

	   pstmt.setString(1,assemblyTrackList[0]);
	   rs = pstmt.executeQuery();
		
		

		if(rs.next())
		{
		 assemblyTrackId = "Exists"	;
		 
		}
 
		  		    
		   }catch(SQLException sql){
			
			sql.printStackTrace();
			throw new ScalarException("checkTestTrackId failed ", sql);

		   } finally {
            if (rs != null) {
                try { rs.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

			return assemblyTrackId;

} 








		
		



}	